package com.devin.controller;

import com.devin.pojo.Role;
import com.devin.pojo.UserInfo;
import com.devin.service.RoleService;
import com.devin.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/getUsers")
//    @Secured("ROLE_ADMIN")
    public ModelAndView getUsers(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                 @RequestParam(value = "size",defaultValue = "5") Integer size){
        List<UserInfo> users = userService.getUsers(page,size);
        PageInfo<UserInfo> userInfoPageInfo = new PageInfo<UserInfo>(users);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userPageInfo",userInfoPageInfo);
        modelAndView.setViewName("pages/users-pages-list");
        return  modelAndView;
    }

    @RequestMapping("/delUser")
    public String delUser(Integer userId){
        userService.delUser(userId);
        return "redirect:/users/getUsers";
    }
    /*删除多选数据*/
    @PostMapping("/delUsersChecked")
    @ResponseBody
    public String delUserChecked(Integer[] ids){
        userService.delUsersChecked(ids);
        return "done";                              /*ajax必须有返回值，否则回调函数error*/
    }

    @RequestMapping("/getRoles2UserAdded")
    public ModelAndView getRoles2UserAdded(){
        List<Role> roles = roleService.getRoles();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roles",roles);
        modelAndView.setViewName("pages/user-add");
        return modelAndView;
    }

    @PostMapping("/addUser")
    public String addUser(UserInfo user,Integer[] roleIds){
        userService.addUser(user,roleIds);
        return "redirect:/users/getUsers";
    }

    @RequestMapping("/getUsersDetail")
    public ModelAndView getUsersDetail(Integer userId){
        UserInfo users = userService.getUsersDetail(userId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",users);
        modelAndView.setViewName("pages/users-detail");
        return modelAndView;
    }

    @RequestMapping("/getRoles2Json")
    @ResponseBody
    public List<Role> getRoles2Json(Integer userId){
        List<Role> roles =roleService.getRoles2Json(userId);
        return roles;
    }

    @RequestMapping("/addRoles")
    public String addRoles(Integer userId,Integer[] roleIds){
        userService.addRoles(userId,roleIds);
        return "redirect:/users/getUsersDetail?userId="+userId;
    }

    @RequestMapping("/delRoleByUserId")
    public String delRoleByUserId(Integer userId,Integer roleId){
        userService.delRoleByUserId(userId,roleId);
        return "redirect:/users/getUsersDetail?userId="+userId;
    }

    @RequestMapping("/first")
    @ResponseBody           /*返回值为空，必须带有@ResponseBody，不然会被thymeleaf解析而报错*/
    public void first(){
        System.out.println("this is first");
        /*间接调用second方法，aop记录日志不会记录访问second的记录，若要记录second的访问记录，开启代理*/
//        second();

        /*使用代理对象，执行second方法，aop就会记录second的访问记录，前提是要开启自动代理*/
        /*@EnableAspectJAutoProxy(exposeProxy = true)*/
        UserController o = (UserController)AopContext.currentProxy();
        o.second();
    }

    public void second(){
        System.out.println("this is second");
    }

}
